package org.seckill.Controller;

import org.seckill.Common.SeckillResult;
import org.seckill.pojo.Seckill;
import org.seckill.pojo.SuccessKilled;
import org.seckill.service.SeckillService;
import org.seckill.vo.Exporter;
import org.seckill.vo.SeckillExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by ASUS on 2017/7/31.
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {

        List<Seckill> seckillList = seckillService.getSeckillAll();
        model.addAttribute("list", seckillList);
        return "list";
    }

    @RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable Long seckillId) {

        if (seckillId == null) {
            return "redirect:/seckill/list";
        }

        Seckill seckill = seckillService.getSeckill(seckillId);
        if (seckill == null) {
            return "forward:/seckill/list";
        }
        model.addAttribute("seckill", seckill);
        return "detail";
    }

    @RequestMapping(value = "/{seckillId}/exprot", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public SeckillResult export(@PathVariable("seckillId") Long seckillId) {
        SeckillResult result = null;
        try {
            Exporter exporter = seckillService.exportSeckillUrl(seckillId);
            result = new SeckillResult(true, exporter);
        } catch (Exception e) {
            result = new SeckillResult(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public SeckillResult execute(@PathVariable("seckillId") Long seckillId, @PathVariable("md5") String md5, @CookieValue(value = "phone", required = false) Long phone) {
        if (phone == null) {
            return new SeckillResult(false, "未注册");
        }
//            SeckillExecution seckillExecution = seckillService.executeSeckill(seckillId,phone,md5);
        SeckillExecution seckillExecution = seckillService.executeSeckillByProcedure(seckillId, phone, md5);
        if (seckillExecution.getStatus()==1){
            return new SeckillResult(true,seckillExecution.getInfo());
        }else {
            return new SeckillResult(false,seckillExecution.getInfo());
        }
    }

    @RequestMapping(value = "/time/now", method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> getTime() {
        Date date = new Date();
        SeckillResult result = new SeckillResult(true, date.getTime());
        return result;
    }

}
